package com.rico.kok.controller.post;

import com.rico.kok.entity.Comment;
import com.rico.kok.entity.CommentLike;
import com.rico.kok.entity.User;
import com.rico.kok.service.interf.CommentLikeService;
import com.rico.kok.service.interf.CommentService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author：Rico_Skipper(JinYang Han)
 * @Date：2022/5/4 13:35
 */
@Controller
public class CommentLikeController {

    @Autowired
    private CommentLikeService commentLikeService;

    @Autowired
    private CommentService commentService;

    //点赞和取消点赞
    @RequiresRoles("user")
    @ResponseBody
    @RequestMapping("/commentLike.htm")
    public String commentLike(@RequestParam("postId") Integer postId, @RequestParam("commentId") Integer commentId, HttpSession session) {
        User user= (User) session.getAttribute("user");
        CommentLike commentLike=new CommentLike();
        commentLike.setCommentPostId(postId);
        commentLike.setCommentUserId(user.getUserId());
        commentLike.setCommentCommentId(commentId);
        Comment comment=new Comment();
        comment.setCommentId(commentId);
        List<Comment> comments = commentService.selectComment(comment);
        Comment likeComment=comments.get(0);
        if (commentLikeService.isCommentLike(commentLike)==0){
            commentLikeService.commentLike(commentLike);
            likeComment.setCommentLike(likeComment.getCommentLike()+1);
            commentService.updateComment(likeComment);
            return "true,"+likeComment.getCommentLike();
        }else {
            commentLikeService.commentNoLike(commentLike);
            likeComment.setCommentLike(likeComment.getCommentLike()-1);
            commentService.updateComment(likeComment);
            return "false,"+likeComment.getCommentLike();
        }
    }
}
